package com.hong.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hong.dto.QueryStudentPageDTO;
import com.hong.dto.QueryStudentResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.hong.domain.Student;
import com.hong.dao.StudentsMapper;
import com.hong.service.StudentService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentsMapper studentsMapper;


    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public int deleteByPrimaryKey(Integer id) {

        redisTemplate.convertAndSend("chat","I am Lazy!!!");

        return studentsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Student record) {
        return studentsMapper.insert(record);
    }

    @Override
    public PageInfo<QueryStudentResultVO> getPage(QueryStudentPageDTO param) {

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<Student> students = studentsMapper.selectList(
                new LambdaQueryWrapper<Student>()
                        .eq(StringUtils.hasLength(param.getName()), Student::getName, param.getName())
                        .eq(StringUtils.hasLength(param.getGender()),Student::getGender, param.getGender()));

        PageInfo<QueryStudentResultVO> result = new PageInfo<>();

        PageInfo<Student> pageInfo = new PageInfo<>(students);


        if (CollectionUtils.isEmpty(pageInfo.getList())){
            return result;
        }
        BeanUtils.copyProperties(pageInfo,result);
        return result;
    }

    @Override
    public Boolean insertOrUpdate(Student record) {

        return null;
    }

    @Override
    public int insertOrUpdateSelective(Student record) {
        return studentsMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(Student record) {
        return studentsMapper.insertSelective(record);
    }

    @Override
    public Student selectByPrimaryKey(Integer id) {



        return studentsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Student record) {
        return studentsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Student record) {
        return studentsMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<Student> list) {
        return studentsMapper.updateBatch(list);
    }

}
